<script lang="ts" setup>
import { useVbenDrawer } from '@vben/common-ui';
import { useVbenForm } from '#/adapter/form';
import { message } from 'ant-design-vue';
import { getCityTreesApi,createApi,editApi } from '#/api/system/region';

defineOptions({
  name: 'FormDrawerDemo',
});

const [Form, formApi] = useVbenForm({
  handleSubmit: onSubmit,
  schema: [
    {
      fieldName: 'id',
      label: 'ID',
      defaultValue: 0,
      component: 'Input',
      dependencies: {
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: [''],
      },
    },
    {
      fieldName: 'pid',
      label: '上级',
      component: 'ApiTreeSelect',
      componentProps: {
        // 菜单接口
        api: getCityTreesApi,
        afterFetch: (data: { id: number; pid: number; name: string; code: string; level: number; }[]) => {
          data.unshift({
            id: 0,
            pid: 0,
            name: '无',
            code: '',
            level: 0,
          });
          return data;
        },
        // 菜单接口转options格式
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        class: 'w-[100%]',
        placeholder: '请选择上级',
        allowClear: false
      },
      defaultValue: 0,
      rules: 'required',
    },
    {
        fieldName: 'name',
        label: '名称',
        component: 'Input',
        componentProps: {
            placeholder: '请输入名称',
        },
        rules: 'required',
    },
    {
        fieldName: 'code',
        label: '区划编码',
        component: 'Input',
        componentProps: {
            placeholder: '请输入区划编码',
        },
        rules: 'required',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: false,
        options: [
          {
            label: '省级',
            value: 1,
          },
          {
            label: '市级',
            value: 2,
          },
          {
            label: '区/县级',
            value: 3,
          },
        ],
        placeholder: '请选择层级',
      },
      fieldName: 'level',
      label: '层级',
      defaultValue: 1,
    },
  ],
  showDefaultActions: false,
});
function onSubmit(values: Record<string, any>) {
  (values.id ? editApi(values) : createApi(values))
    .then((res) => {
      message.success({
        content: res.message,
      });
      drawerApi.close();
    })
    .catch(() => {
      drawerApi.unlock();
    });
}
const [Drawer, drawerApi] = useVbenDrawer({
  onCancel() {
    drawerApi.close();
  },
  onConfirm: async () => {
    await formApi.validateAndSubmitForm();
    drawerApi.lock();
  },
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const { values } = drawerApi.getData<Record<string, any>>();
      if (values.id) {
        formApi.setValues(values);
        drawerApi.setState({ title: '编辑' })
      }else{
        formApi.setValues({
          pid: 0,
          status: 1,
          meta: {
            title: '',
            icon: '',
            hideMenu: false,
            hideChildrenInMenu: false,
            hideBreadcrumb: false,
            currentActiveMenu: '',
          },
        });
        drawerApi.setState({ title: '新增' })
      }
    }
  },
  title: '新增',
  overlayBlur: 1,
});
</script>
<template>
  <Drawer>
    <Form />
  </Drawer>
</template>
